package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.UserPayService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.UserPay;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author weizhuang
 *  用户支付设置
 */
@RestController
@RequestMapping("/userPay")
public class UserPayController {


    @Resource
    private UserPayService userPayService;


    /**
     * 查找用户支付信息
     * @param userPay 用户账户
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getUserPayById(@RequestBody UserPay userPay){
        try {
            UserPay byId = userPayService.findById(userPay.getId() + "");
            return ConvenientResult.build(200,"成功",byId);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 设置用户支付信息
     * @param userPay 用户账户
     * @return Result结果集
     */
    @RequestMapping("/saveOrUpdate")
    public ConvenientResult getUserPaySaveOrUpdate(@RequestBody UserPay userPay){
        try {
            UserPay byId = userPayService.findByUserId(userPay.getUserId());
            if(byId == null){
                //该用户第一次设置 插入一条记录
                userPayService.save(userPay);
            }else{
                //该用户不是第一次设置 更新记录
                userPay.setId(byId.getId());
                userPayService.update(userPay);
            }
            return ConvenientResult.build(200,"成功",userPay);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }




}
